This page used to be called "[Testing your ActiveAdmin controllers with RSpec](https://github.com/activeadmin/activeadmin/wiki/Testing-your-ActiveAdmin-controllers-with-RSpec)"

## The problem

During the creation of the gem [ActiveTrail](github.com/fernandes/activetrail) integrating ActiveAdmin with Trailblazer the controller was customized, something like this:

```ruby
ActiveAdmin.register Book do
  # everything happens here :D
  controller do
    include ActiveTrail::Controller
  end
end
```

And how to test the controller and make some requests? The routes created dynamically by AA wasn't available when running the test suite, so I inspected AA test suite to see how its done, and here is how the problem was solved:

## The solution

This solution can be checked on this [commit](https://github.com/fernandes/activetrail/commit/064729d533a17263d69503186e987040ae02ecba)

Supposing that we want to make a simple test like this:

```ruby
RSpec.describe Admin::BooksController, :type => :controller do 
  describe "GET index" do
    it "assigns books" do
      book = Book.create
      get :index
      expect(assigns(:books)).to eq([book])
    end
  end
end
```

### Combustion

As we are testing a gem that uses Rails, we used Pat's [Combustion](https://github.com/pat/combustion) gem to create a dummy Rails application inside our gem so we can make controllers and requests tests, check the gem page and our commit how to set it up.

1. Setup Combustion
2. Add a `spec/internal/config/initializers/activeadmin.rb` file
3. Create resource and comments table on `spec/internal/db/schema.rb`
4. Edit rspec `spec/spec_helper.rb` and `spec/support/integration_example_group.rb`

### Spec Helper

I copied spec helper and integration example group from AA repo, so we have [load_default!](https://github.com/fernandes/activetrail/commit/064729d533a17263d69503186e987040ae02ecba#diff-93830fa29d616f7c87903d08b5b1b29aR29) and [reload_routes!](https://github.com/fernandes/activetrail/commit/064729d533a17263d69503186e987040ae02ecba#diff-93830fa29d616f7c87903d08b5b1b29aR43) methods available.

Then in our [rspec config](https://github.com/fernandes/activetrail/commit/064729d533a17263d69503186e987040ae02ecba#diff-93830fa29d616f7c87903d08b5b1b29aR117) we can load AA stuff [before](https://github.com/fernandes/activetrail/commit/064729d533a17263d69503186e987040ae02ecba#diff-93830fa29d616f7c87903d08b5b1b29aR119) any test and all routes and resources will be available.

In activetrail case it was added on rspec config because we just have AA tests, but if you have more, just run this before(:each) on the specific tests.

Check the full [spec_helper.rb](https://github.com/fernandes/activetrail/blob/064729d533a17263d69503186e987040ae02ecba/spec/spec_helper.rb) and [integration_example_group.rb](https://github.com/fernandes/activetrail/blob/064729d533a17263d69503186e987040ae02ecba/spec/support/integration_example_group.rb) for a complete example.

ps: After you got controllers tests working, requests will be ok too.
